package pure.engine.model 
{
	import flash.display.BitmapData;
	import flash.filters.BitmapFilter;
	import flash.geom.ColorTransform;
	import flash.geom.Matrix;
	import pure.engine.core.ITickListener;
	import pure.engine.core.ProcessManager;
	import pure.engine.core.VisibleCore;
	import pure.engine.world.Fragment;
	import pure.engine.core.ns_despair;
	use namespace ns_despair;
	
	/**
	 * 渲染器抽象类
	 * [Author] - PURE
	 * 
	 * [Hint] - 渲染器初期化时，要做一次强制更新.
	 */
public class Renderer extends VisibleCore implements IRenderer
{
	
	
	//======================
	// Const
	//======================
	
	
	ns_despair static const r_dirtyTransformFlag:uint  =  0x01;  // 形变
	ns_despair static const r_dirtyImageFlag:uint      =  0x02;  // 脏画像
	ns_despair static const r_dirtyRangeFlag:uint      =  0x04;  // 脏偏移
	
	ns_despair static const r_invisibleFlag:uint       =  0x10;  // 不可见
	ns_despair static const r_noRenderFlag:uint        =  0x20;  // 渲染停止
	ns_despair static const r_uninitializedFlag:uint   =  0x40;  // 未初期化
	
	ns_despair static const r_addedToModifyFlag:uint   =  0x100;  // 已加入脏列表
	
	
	//======================
	// Property
	//======================
	
	
	/**
	 * 是否可见
	 */
	public function get visible():Boolean 
	{ 
		return !Boolean(m_flags & r_invisibleFlag);
	}

	public function set visible(b:Boolean):void
	{
		if(b)
		{
			m_flags &= ~r_invisibleFlag;
			if (!Boolean(m_flags & 0xFF0) && Boolean(m_flags & 0x0F))
			{
				RenderManager.addDirtyRenderer(this);
				m_flags |= r_addedToModifyFlag;
			}
		}
		else
		{
			if (!Boolean(m_flags & 0xFF0))
			{
				RenderManager.addDirtyRenderer(this);
			}
			m_flags |= r_invisibleFlag;
		}
	}

	
	/**
	 * 初期化
	 */
	public function initialize():void
	{
		m_scaleX = m_scaleY = m_alpha = 1;
		m_flags = r_uninitializedFlag;
		m_fragment = Fragment.getFragment();
	}
	
	
	/**
	 * 透明度
	 */
	public function get alpha():Number
	{ 
		return this.m_alpha;
	}
	
	public function set alpha(v:Number):void 
	{
		this.m_fragment.alpha = this.m_alpha = (v > 1) ? 1 : (v < 0) ? 0 : v;
		_setRendererFlag(r_dirtyTransformFlag)
	}
	

	/**
	 * 缩放x
	 */
	public function get scaleX():Number
	{ 
		return this.m_scaleX; 
	}
	
	public function set scaleX(v:Number):void 
	{
		this.m_fragment.scaleX = this.m_scaleX = (v < 0) ? 0 : v;
		_setRendererFlag(r_dirtyTransformFlag)
	}
	
	
	/**
	 * 缩放y
	 */
	public function get scaleY():Number 
	{
		return this.m_scaleY;
	}
	
	public function set scaleY(v:Number):void
	{ 
		this.m_fragment.scaleY = this.m_scaleY = (v < 0) ? 0 : v;
		_setRendererFlag(r_dirtyTransformFlag)
	}

	
	/**
	 * [Override]
	 * 
	 * 角度
	 * 
	 * [Hint] - 0 ~ 360
	 */
	override public function set rotation(v:Number):void 
	{
		this.m_fragment.rotation = this.m_rotation = Boolean(v % 360 < 0) ? v % 360 + 360 : v % 360;
		_setRendererFlag(r_dirtyTransformFlag)
	}
	
	
	/**
	 * [Override]
	 * 弧度
	 */
	override public function set angle(v:Number):void 
	{ 
		super.angle = v;
		_setRendererFlag(r_dirtyTransformFlag)
	}

	
	/**
	 * [Override]
	 * 
	 * 加入滤镜
	 * 
	 * [Param] - F  滤镜
	 */
	override public function addFilter(F:BitmapFilter):void
	{
		super.addFilter(F);
		m_fragment.filters = m_filterList;
	}
	
	
	/**
	 * [Override]
	 * 
	 * 清空滤镜
	 */
	override public function clearFilter():void
	{
		super.clearFilter();
		m_fragment.filters = null;
	}
	
	
	
	/**
	 * 初期化
	 * 
	 * [Hint] - 第一次加入世界时使用.
	 */
	ns_despair function addToWorld(b:Boolean):void
	{
		if(!b)
		{
			m_flags |= r_noRenderFlag;
		}
		
		m_flags &= ~r_uninitializedFlag;
		
		var i:int;

		// 不可见
		if (m_flags & r_invisibleFlag)
		{
			m_fragment.invalidate();
		}
		
		// 偏移
		else
		{
			m_fragment.updateAll(_generatePrimaryImage(), m_x, m_y, Boolean(m_rotation != 0))// || m_scaleX != 1 || m_scaleY != 1));
		}
	}

	
	/**
	 * 更正
	 */
	ns_despair function modify():void 
	{
		var range:Number, i:int;
		
		// 不可见
		if (m_flags & r_invisibleFlag)
		{
			m_fragment.invalidate();
		}
		
		// 偏移
		else if(m_flags & r_dirtyRangeFlag)
		{
			m_fragment.updateAll(_generatePrimaryImage(), m_x, m_y, Boolean(m_rotation != 0))// || m_scaleX != 1 || m_scaleY != 1));
			//m_fragment.updateAll(_generatePrimaryImage(), m_x, m_y, Boolean(m_rotation != 0 || m_scaleX != 1 || m_scaleY != 1));
		}
		
		// 画像
		else if(m_flags & r_dirtyImageFlag)
		{
			m_fragment.updateImage(_generatePrimaryImage(), Boolean(m_rotation != 0))// || m_scaleX != 1 || m_scaleY != 1));
			//m_fragment.updateImage(_generatePrimaryImage(), Boolean(m_rotation != 0 || m_scaleX != 1 || m_scaleY != 1));
		}
		
		// 形变
		else
		{
			m_fragment.updateTransform(Boolean(m_rotation != 0))// || m_scaleX != 1 || m_scaleY != 1));
			//m_fragment.updateTransform(Boolean(m_rotation != 0 || m_scaleX != 1 || m_scaleY != 1));
		}
		
		// 重置
		m_flags &= (r_invisibleFlag | r_noRenderFlag);
	}
	
	
	/**
	 * 开启渲染
	 * 
	 * [Hint] - 进入渲染区域时使用.
	 */
	ns_despair function enable():void
	{
		m_flags &= ~r_noRenderFlag;
		if (!Boolean(m_flags & 0xFF0) && Boolean(m_flags & 0x0F))
		{
			RenderManager.addDirtyRenderer(this);
			m_flags |= r_addedToModifyFlag;
		}
	}
	
	
	/**
	 * 阻止渲染
	 * 
	 * [Hint] - 移出渲染区域时使用.
	 */
	ns_despair function disable():void
	{
		m_flags |= r_noRenderFlag;
	}
	
	
	/**
	 * 毁灭
	 */
	override ns_despair function destroy():void
	{
		if (Boolean(m_flags & r_addedToModifyFlag))
		{
			RenderManager.removeDirtyRenderer(this);
		}
		m_fragment.recycle(Boolean(m_rotation != 0 || m_scaleX != 1 || m_scaleY != 1 || m_alpha != 1 || m_filterList));
		m_fragment = null;
		
		super.destroy();
	}
	
	
	
	
	/* ----------------------------------------------------------------------------------------------------------------
	 * ★★★★★★★★★★★★★★★★★★★★★  private  ★★★★★★★★★★★★★★★★★★★★★
	 * ----------------------------------------------------------------------------------------------------------------
	 */
	
	
	
	
	/**
	 * 设置脏渲染器
	 */
	ns_despair function _setRendererFlag(flag:int):void
	{
		if (!Boolean(m_flags & 0xFF0))
		{
			RenderManager.addDirtyRenderer(this);
			m_flags |= r_addedToModifyFlag;
		}
		m_flags |= flag;
	}
	
	
	/**
	 * 画像缓存
	 */
	ns_despair function _generatePrimaryImage():BitmapData
	{
		return null;
	}
	
	
	
	
	//======================
	// Member
	//======================
	
	
	ns_despair var m_alpha:Number;
	
	ns_despair var m_scaleX:Number;
	
	ns_despair var m_scaleY:Number;
	
	ns_despair var m_fragment:Fragment;
}

}